<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!--
      通过 v-model 绑定当前激活标签对应的索引值，默认情况下启用第一个标签
      通过 animated 属性可以开启切换标签内容时的转场动画
      通过 swipeable 属性可以开启滑动切换标签页
     -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab
        :title="channel.name"
        v-for="channel in pindao"
        :key="channel.id"
      >
        <ArticleList :channel="channel"></ArticleList>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <!--  -->
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChennelEditShow = true"
      >
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!-- /频道列表 -->
    <!-- closeable:是否显示关闭图标 -->
    <van-popup
      v-model="isChennelEditShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <ChannelEdit
        :myChannels="pindao"
        :active="active"
        @updateActive="onUpdateActive"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from "@/api/user";
import ArticleList from "./components/article-list";
import ChannelEdit from "./components/channel-edit";
import { mapState } from "vuex";
import { getItem } from "@/utils/storage";
export default {
  name: "HomeIndex",
  components: {
    ArticleList,
    ChannelEdit,
  },
  props: {},
  data() {
    return {
      // 标识所选哪一个频道
      active: 0,
      pindao: [],
      isChennelEditShow: false,
    };
  },
  computed: {
    ...mapState(['user'])
    
  },
  watch: {},
  created() {
    this.pindaos();
  },
  mounted() {},
  methods: {
    async pindaos() { 
      // debugger
      // try {
     
      //   let pindao =[]
        
      //   if (this.user) {
      //     const { data } = await getUserChannels();
      //     // console.log(data);
      //     console.log(data.data.channels);
      //     pindao = data.data.channels
      //     console.log(channels);
      //     // this.pindao = data.data.channels;
      //   } else {
      //     const xxc = getItem("TOUTIAO_CHANNELS");
      //     // 本地有数据
      //     if (xxc) {
      //       pindao = xxc;
      //     } else {
      //       const { data } = await getUserChannels();
      //      pindao = data.data.channels;
      //     }
      //   }
      //   this.pindao = pindao
      //   // console.log(piao);
      // } catch (err) {
      //   console.log(err);
      //   this.$toast("获取用户频道失败");
      // }


       try {
        // debugger
        
        let channels = []
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (this.user || !localChannels) {
            // 登录 或者 未登录本地没有存储 获取后端数据
            const { data } = await getUserChannels()
            channels = data.data.channels
        } else {
            // 未登录并且本地有数据
            channels = localChannels
        }
        this.pindao = channels
    } catch (err) {
        this.$toast('获取频道列表数据失败')
    }
    },
    onUpdateActive(index, isChennelEditShow = true) {
      //更新索引
      this.active = index;
      //关闭编辑频道弹出层
      this.isChennelEditShow = isChennelEditShow;
    },
  },
};
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  // /deep/
  .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  //给父节点添加deep，这样下面的都生效，因为下面的都需要，所以给父亲添加就可以了
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      // 翻2倍
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }

    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }

    .van-tab--active {
      color: #333333;
    }

    .van-tabs__nav {
      padding-bottom: 0;
    }

    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }

    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }

    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      background-color: rgba(255, 255, 255, 0.902);
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 58px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
